<#assign module="marketing"/>

<@override name="header">
<link rel="stylesheet" href="${ctx}/assets/css/uploadify.css">
<link rel="stylesheet" href="${ctx}/assets/css/datetimepicker.css">
</@override>
<@override name="body">

	<div class="row">
		<div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading">
					添加新活动
					<label class="control-label"><font color="red">（带*表示必填项）</font></label>
			    </div>
			    <div class="panel-body">
    				<form id="submit_form" action="${ctx}/activity/create" class="form-horizontal" method="post">
		               
		               	<@shiro.isMainCompany>
		               		<div class="form-group">
			                    <label class="col-sm-3 control-label"><em>*</em>所属公司：</label>
			                    <div class="col-sm-3">
			                    	<select class="form-control" id="companyId" name="companyId" required>
			                    		<option value="">--请选择--</option>
			                    		<option value="0">所有公司</option>
			                    		<#if companys??>
			                    			<#list companys as company>
			                    				<option value="${company.id}">${company.abbreviation?if_exists}</option>
			                    			</#list>
			                    		</#if>
			                    	</select>
			                    </div>
			                </div>
		               	</@shiro.isMainCompany>
		               
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>活动名称：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="title" name="title" type="text" maxlength="7" required/>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>活动对象：</label>
		                    <div class="col-sm-3">
		                    	<select class="form-control" id="activityTarget" name="activityTarget" required>
		                    		<option value="">--请选择--</option>
		                    		<option value="all">所有客户</option>
		                    		<option value="passenger">个人客户</option>
		                    		<option value="enterprise">企业客户</option>
		                    	</select>
		                    </div>
		                    
	                    	<label style="float:left;" class="control-label"><em>*</em>用户来源：</label>
		                    <div class="col-sm-2">
		                    	<select class="form-control" id="memberFromSource" name="memberFromSource">
		                    		<option value="">所有来源</option>
		                    		<option value="APP">APP</option>
		                    		<option value="电话下单">电话下单</option>
		                    		<option value="微信">微信</option>
		                    		<option value="客户经理">客户经理</option>
		                    		<option value="网站下单">网站下单</option>
		                    		<option value="补单">补单</option>
		                    	</select>
		                    </div>
		                    
		                </div>
		                
		                <div class="form-group">
		                	<input type="hidden" id="gradeName" name="gradeName" />
		                    <label class="col-sm-3 control-label"><em>*</em>客户等级：</label>
		                    <div class="col-sm-3">
		                    	<select class="form-control" id="gradeId" name="gradeId">
		                    		<option value="">--所有等级--</option>
		                    		<#if grades??>
		                    			<#list grades as grade>
		                    				<option value="${grade.id}">${grade.name?if_exists}</option>
		                    			</#list>
		                    		</#if>
		                    	</select>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>活动事件：</label>
		                    <div class="col-sm-3">
		                    	<select class="form-control" id="activityEvent" name="activityEvent" required>
		                    		<option value="">--请选择--</option>
		                    		<option value="newRegister">新注册</option>
		                    		<option value="discount">代驾/专车打折</option>
		                    		<option value="orderComplete">订单结算</option>
		                    		<option value="shareSuccess">分享成功</option>
		                    		<option value="orderCompleteTimes">完成代驾</option>
		                    		<option value="recharging">充值</option>
		                    	</select>
		                    </div>
		                    
		                    <div id="discount_div" style="display:none;">
		                    	<label style="float:left;" class="control-label"><em>*</em></label>
			                    <div class="col-sm-2">
			                    	<input class="form-control" id="discount" name="discount" type="text"/>
			                    </div>
			                    <label style="float:left;" class="control-label">折</label>
		                    </div>
		                    
		                    <div id="orderCompleteTimes_div" style="display:none;">
		                    	<label style="float:left;" class="control-label"><em>*</em>每满</label>
			                    <div class="col-sm-2">
			                    	<input class="form-control" id="completeTimes" name="completeTimes" type="number" min="1"/>
			                    </div>
			                    <label style="float:left;" class="control-label">次</label>
		                    </div>
		                    
		                    <div id="shareTimes_div" style="display:none;">
			                    <label style="float:left;" class="control-label"><em>*</em></label>
			                    <div class="col-sm-2">
			                    	<input class="form-control" id="shareTimes" name="shareTimes" type="number" min="1"/>
			                    </div>
			                    <label style="float:left;" class="control-label">次内</label>
		                    </div>
		                    
							<div id="recharging_div" style="display:none;">
		                    	<label style="float:left;" class="control-label"><em>*</em>满</label>
			                    <div class="col-sm-2">
			                    	<input class="form-control" id="rechargingMoney" name="rechargingMoney" type="text"/>
			                    </div>
			                    <label style="float:left;" class="control-label">元</label>
		                    </div>
		                    
		                </div>
		                
		                <div class="form-group" id="orderRelation_div" style="display:none;">
		                    <label class="col-sm-3 control-label"><em>*</em>订单来源：</label>
		                    <div class="col-sm-3">
		                    	<select class="form-control" id="orderFromSource" name="orderFromSource">
		                    		<option value="">所有来源</option>
		                    		<option value="APP下单">APP下单</option>
		                    		<option value="电话下单">电话下单</option>
		                    		<option value="微信下单">微信下单</option>
		                    		<option value="客户经理">客户经理</option>
		                    		<option value="网站下单">网站下单</option>
		                    		<option value="补单">补单</option>
		                    	</select>
		                    </div>
		                    <#if zhuanMethod() >
			                    <label style="float:left;" class="control-label"><em>*</em>订单类型</label>
			                    <div class="col-sm-3">
			                    	<select class="form-control" id="orderCarType" name="orderCarType">
			                    		<option value="all">全部</option>
			                    		<option value="daijia">代驾</option>
			                    		<option value="zhuanche">专车</option>
			                    	</select>
			                    </div>
		                    <#else>
								<input type="hidden" name="orderCarType" id="orderCarType" value="daijia" id="orderCarType"/>   
	                		</#if>
		                </div>
		                
		                <div class="form-group" id="activityType_div">
		                    <label class="col-sm-3 control-label"><em>*</em>活动类型：</label>
		                    <div class="col-sm-3">
		                    	<select class="form-control" id="activityType" name="activityType" required>
		                    		<option value="">--请选择--</option>
		                    		<option value="cash">返现</option>
		                    		<option value="coupon">返劵</option>
		                    	</select>
		                    </div>
		                    
		                    <div id="cash_div" style="display:none;">
			                    <label style="float:left;" class="control-label"><em>*</em>返现金额</label>
			                    <div class="col-sm-3">
			                    	<input class="form-control" id="money" name="money" type="text"/>
			                    </div>
		                    </div>
		                    <div id="coupon_div" style="display:none;">
			                    <label style="float:left;" class="control-label"><em>*</em>优惠劵</label>
			                    <div class="col-sm-3">
			                    	<select class="form-control" id="couponRuleId" name="couponRuleId">
			                    		<option value="">--请选择--</option>
			                    		<#if coupons??>
				                    		<#list coupons as coupon>
				                    			<option value="${coupon.id}">
				                    				${coupon.title?if_exists}
				                    				<#if coupon.timeStatus==0>
														(未开始)
													<#elseif coupon.timeStatus==1>
														(使用中)
													</#if>
				                    			</option>
				                    		</#list>
			                    		</#if>
			                    	</select>
			                    </div>
		                    </div>
		                    
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>有效期：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="start" name="start" type="text" required/>
		                    </div>
		                    <label style="float:left;" class="control-label">-</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="end" name="end" type="text" required/>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>活动图片：</label>
		                    <div class="col-sm-6">
		                    	<input type="hidden" id="picture" name="picture" />
					          	<input type="file" id="picture_file" style="display:none;"> 建议图片大小 600×200
					            <p>
					            	<div id="queue"></div>
					            </p>
					            <p>
					            	<img id="picture_show" style="display:none;width:300px;height:100px;" />
					          	</p>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label">立即推送给客户：</label>
		                    <div class="col-sm-3">
		                    	<p class="form-control-static">
			                    	<label style="padding-right:12px;">
			                    		<input type="checkbox" name="pushNow" checked="checked"/>
			                    	</label>
		                    	</p>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label">活动内容：</label>
		                    <div class="col-sm-7">
		                    	<textarea class="form-control" cols="20" id="content" name="content" rows="2"></textarea>
		                    </div>
		                </div>
		                
		                <div class="form-group" style="margin-left:10px;">
		                	<label class="col-sm-3 control-label"></label>
			                <input id="submit_button" type="submit" class="btn btn-primary" value="保存">
			                <input type="button" class="btn btn-default" onclick="window.location.href = '${ctx}/activity/list';" value="返回">
			            </div>
			            
					</form>
        		</div>
			</div>
		</div>
	</div>

</@override>
<@override name="footer">
<script src="${ctx}/assets/js/jquery.uploadify.js"></script>
<script src="${ctx}/assets/kindeditor/kindeditor-all-min.js"></script>
<script src="${ctx}/assets/kindeditor/lang/zh_CN.js"></script>
<script src="${ctx}/assets/js/bootstrap-datetimepicker.min.js"></script>
<script src="${ctx}/assets/js/bootstrap-datetimepicker.zh-CN.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
	
		$("#start").datetimepicker({ format: 'yyyy-mm-dd HH:ii', language: 'zh-CN', minView: 'hour', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});
		$("#end").datetimepicker({ format: 'yyyy-mm-dd HH:ii', language: 'zh-CN', minView: 'hour', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});
		
		KindEditor.ready(function(K) {
			window.editor = K.create('#content',{
				filterMode : false,
				uploadJson : '${ctx}/common/kindeditor/fileupload',
				allowUpload : true,
				fileManagerJson : '${ctx}/common/kindeditor/filemanager',
				allowFileManager : true,
				height:400,
				resizeType:0,
				afterCreate : function() {
					var self = this;
					K.ctrl(document, 13, function() {
						self.sync();
					});
					K.ctrl(self.edit.doc, 13, function() {
						self.sync();
					});
				},
				afterBlur:function(){ 
					this.sync();
				}
			});
		});
		
		setTimeout(function(){
			$('#picture_file').uploadify({
				'swf': '${ctx}/assets/js/uploadify.swf',
				'uploader': '${ctx}/common/uploadimg.json',
				'cancelImg': '${ctx}/assets/css/images/uploadify-cancel.png',
				'fileObjName':'imgFile',
				'fileTypeExts':'*.jpg;*.jpeg;*.png;*.gif;*.bmp;',
				'buttonText':'上传活动图片',
				'onUploadProgress':function(thisUP, file, fileBytesLoaded, fileTotalBytes, queueBytesLoaded, uploadSize){
					
				},
				'onUploadSuccess':function(thisUP, file, data, response){
					var jsonobj = eval('(' + file + ')');
					if(jsonobj.success){
		        		$('#picture_show').attr("src","${ctx}/"+jsonobj.url);
		        		$("#picture_show").fadeIn();
			        	$("#picture").val(jsonobj.url);
		        	}
		        	else{
		        		alert(jsonobj.msg);
		        	}
				}
			});
		},10);
		
		
		$("#submit_form").submit(function(){
			$("#submit_button").attr("disabled",true);
			
			if($("#activityEvent").val()=="discount"){
				var discount=$("#discount").val();
				var reg=/^(-?\d+)(.[0-9]{0,1})?$/;
				if(!reg.test(discount) || discount<=0 || discount>=10){
			       $.scojs_message('无效的折扣', $.scojs_message.TYPE_ERROR);
			       $("#submit_button").attr("disabled",false);
			       $("#discount").focus();
			       return false;
			    }
			}
			
			if($("#activityEvent").val()=="recharging"){
				var rechargingMoney=$("#rechargingMoney").val();
				var reg=/^(-?\d+)(.[0-9]{0,2})?$/;
				if(!reg.test(rechargingMoney)){
			       $.scojs_message('无效的金额，金额必须为数字', $.scojs_message.TYPE_ERROR);
			       $("#submit_button").attr("disabled",false);
			       $("#rechargingMoney").focus();
			       return false;
			    }
			}
			
			if($("#activityType").val()=="cash" && $("#activityEvent").val()!="discount"){
				var money=$("#money").val();
				var reg=/^(-?\d+)(.[0-9]{0,2})?$/;
				if(!reg.test(money)){
			       $.scojs_message('无效的金额，金额必须为数字', $.scojs_message.TYPE_ERROR);
			       $("#submit_button").attr("disabled",false);
			       $("#money").focus();
			       return false;
			    }
			}
			
			if($("#start").val()>=$("#end").val()){
		    	$.scojs_message('结束时间必须大于开始时间', $.scojs_message.TYPE_ERROR);
		    	$("#submit_button").attr("disabled",false);
		    	return false;
		    }
		    
		    if($("#picture").val()==""){
		    	$.scojs_message('请上传活动图片', $.scojs_message.TYPE_ERROR);
		    	$("#submit_button").attr("disabled",false);
		    	return false;
		    }
		    
			return true;
		});
		
		$("#activityType").change(function(){
			$("#cash_div").hide();
			$("#coupon_div").hide();
			$("#money").val("");
			$("#couponRuleId option")[0].selected="selected";
			if($(this).val()=="cash"){
				$("#cash_div").fadeIn(200);
				$("#money").attr("required",true);
				$("#couponRuleId").attr("required",false);
			}
			else if($(this).val()=="coupon"){
				$("#coupon_div").fadeIn(200);
				$("#couponRuleId").attr("required",true);
				$("#money").attr("required",false);
			}
		});
		
		$("#activityEvent").change(function(){
		
			if($(this).val()=="discount"){//如果选择打折
				$("#activityType").attr("required",false);
			
				$("#activityType_div").slideUp();//隐藏活动类型
				$("#discount_div").fadeIn(200);//显示折扣
				$("#discount").attr("required",true);
				
				$("#shareTimes_div").hide();
				$("#shareTimes").attr("required",false);
				
				$("#orderCompleteTimes_div").hide();
				$("#completeTimes").attr("required",false);
				
				$("#recharging_div").hide();
				$("#rechargingMoney").attr("required",false);
				
				$("#orderRelation_div").slideDown();
				
			}
			else{
				$("#activityType").attr("required",true);
			
				$("#activityType_div").slideDown();//显示活动类型
				$("#discount_div").hide();//隐藏折扣
				$("#discount").attr("required",false);
			
				$("#joinTimes").val("");
			
				if($(this).val()=="shareSuccess"){
					$("#shareTimes_div").fadeIn(200);
					$("#shareTimes").attr("required",true);
				}
				else{
					$("#shareTimes_div").hide();
					$("#shareTimes").attr("required",false);
				}
				
				if($(this).val()=="orderComplete" || $(this).val()=="orderCompleteTimes"){
					$("#orderRelation_div").slideDown();
				}
				else{
					$("#orderRelation_div").slideUp();
				}
				
				if($(this).val()=="orderCompleteTimes"){
					$("#orderCompleteTimes_div").fadeIn(200);
					$("#completeTimes").attr("required",true);
				}
				else{
					$("#orderCompleteTimes_div").hide();
					$("#completeTimes").attr("required",false);
				}
				
				if($(this).val()=="recharging"){
					$("#recharging_div").fadeIn(200);
					$("#rechargingMoney").attr("required",true);
				}
				else{
					$("#recharging_div").hide();
					$("#rechargingMoney").attr("required",false);
				}
			}
			
			
		});
		
		$("#gradeId").change(function(){
			$("gradeName").val($(this.val()));
		});
	});
</script>
</@override>
<@extends name="../layout.ftl"/>